#include "convert.h"

roPreImage* roConvert::RGB2HSV(roPreImage* source, bool rewrite)
{
	uchar* data = source->data(), *datacopy;
	uint width = source->width(), height = source->height();
	if(rewrite)
	{
		for (uint y = 0; y < height; ++ y)
			for (uint x = 0; x < width; ++ x)
			{
				long xy = (y * width + x);
				long pos = xy * 3;
				roPixelTransform::RGB2HSV(
					data[pos], data[pos + 1], data[pos + 2],
					data[pos], data[pos + 1], data[pos + 2]
				);

			}
		return source;
	}
	else
	{
		datacopy = new uchar[width * height * 3];
		for (uint y = 0; y < height; ++ y)
			for (uint x = 0; x < width; ++ x)
			{
				long xy = (y * width + x);
				long pos = xy * 3;
				roPixelTransform::RGB2HSV(
					datacopy[pos], datacopy[pos + 1], datacopy[pos + 2],
					data[pos], data[pos + 1], data[pos + 2]
				);

			}
		return new roPreImage(datacopy, width, height);
	}
	return 0;
}

roPreImage* roConvert::HSV2RGB(roPreImage* source, bool rewrite)
{
	uchar* data = source->data(), *datacopy;
	uint width = source->width(), height = source->height();
	if(rewrite)
	{
		for (uint y = 0; y < height; ++ y)
			for (uint x = 0; x < width; ++ x)
			{
				long xy = (y * width + x);
				long pos = xy * 3;
				roPixelTransform::HSV2RGB(
					data[pos], data[pos + 1], data[pos + 2],
					data[pos], data[pos + 1], data[pos + 2]
				);

			}
		return source;
	}
	else
	{
		datacopy = new uchar[width * height * 3];
		for (uint y = 0; y < height; ++ y)
			for (uint x = 0; x < width; ++ x)
			{
				long xy = (y * width + x);
				long pos = xy * 3;
				roPixelTransform::HSV2RGB(
					datacopy[pos], datacopy[pos + 1], datacopy[pos + 2],
					data[pos], data[pos + 1], data[pos + 2]
				);

			}
		return new roPreImage(datacopy, width, height);
	}
	return 0;
}

roPreImage* roConvert::RGB2HSL(roPreImage* source, bool rewrite)
{
	uchar* data = source->data(), *datacopy;
	uint width = source->width(), height = source->height();
	if(rewrite)
	{
		for (uint y = 0; y < height; ++ y)
			for (uint x = 0; x < width; ++ x)
			{
				long xy = (y * width + x);
				long pos = xy * 3;
				roPixelTransform::RGB2HSL(
					data[pos], data[pos + 1], data[pos + 2],
					data[pos], data[pos + 1], data[pos + 2]
				);

			}
		return source;
	}
	else
	{
		datacopy = new uchar[width * height * 3];
		for (uint y = 0; y < height; ++ y)
			for (uint x = 0; x < width; ++ x)
			{
				long xy = (y * width + x);
				long pos = xy * 3;
				roPixelTransform::RGB2HSL(
					datacopy[pos], datacopy[pos + 1], datacopy[pos + 2],
					data[pos], data[pos + 1], data[pos + 2]
				);

			}
		return new roPreImage(datacopy, width, height);
	}
	return 0;
}

roPreImage* roConvert::HSL2RGB(roPreImage* source, bool rewrite)
{
	uchar* data = source->data(), *datacopy;
	uint width = source->width(), height = source->height();
	if(rewrite)
	{
		for (uint y = 0; y < height; ++ y)
			for (uint x = 0; x < width; ++ x)
			{
				long xy = (y * width + x);
				long pos = xy * 3;
				roPixelTransform::HSL2RGB(
					data[pos], data[pos + 1], data[pos + 2],
					data[pos], data[pos + 1], data[pos + 2]
				);

			}
		return source;
	}
	else
	{
		datacopy = new uchar[width * height * 3];
		for (uint y = 0; y < height; ++ y)
			for (uint x = 0; x < width; ++ x)
			{
				long xy = (y * width + x);
				long pos = xy * 3;
				roPixelTransform::HSL2RGB(
					datacopy[pos], datacopy[pos + 1], datacopy[pos + 2],
					data[pos], data[pos + 1], data[pos + 2]
				);

			}
		return new roPreImage(datacopy, width, height);
	}
	return 0;
}